/**
 * Created by chenzheyu on 2017/4/5.
 */


$(function () {

    var limit = 20;
    var SEOVal = '';
    var State = '';

    function PagingMd(index, SEOVal, Refresh, State) {
        option = {
            nameOrPhone: SEOVal || '',
            page: index,
            isPass: State || '',
            'limit': limit,
            "pager.offset": (index - 1) * limit
        };
        $.ajax({
            url: ctx + '/information/selectInforMation',
            data: option,
            type: 'GET',
            dataType: "json",
            success: function (data) {
                var Data = data;
                var OrderOption = {//表格数据参数
                    data: {
                        Thead: {
                            result: '是否通过',
                            name: '姓名',
                            phone: '手机',
                            idcard: '身份证',
                            drivernumber: '驾驶证',
                            photourl: '证件照',
                            registrationtime: '注册时间',
                            action: '操作'
                        },
                        Tbody: []
                    },
                    OrderH: '500px',
                    select: true,
                    selectchange: function (index) {//多选
                        var state = true;
                        var IdPass = [];
                        for (var i = 0; i < index.length; i++) {
                            IdPass.push(Data.content[index[i]].id)
                        }
                        var IdPassSt = '';
                        if (IdPass.length > 1) {
                            IdPassSt = IdPass.join(",");
                        } else {
                            IdPassSt += IdPass[0]
                        }

                        if (IdPassSt=='undefined') {
                            state = false
                        }
                        $('.BatchPass').unbind('click')
                        $('.BatchPass').on('click', function (e) {
                            e.stopPropagation();
                            if (state) {
                                state = false;
                                $.ajax({
                                    url: ctx + '/information/batchpass',
                                    type: 'POST',
                                    data: {
                                        size: IdPassSt
                                    },
                                    success: function (data) {
                                        if (data.message == '修改成功') {
                                            console.log(IdPassSt)
                                            new $().Overlays({data: {text: '批量通过成功！', state: 'Ok'}});
                                            PagingMd(1, SEOVal);
                                            state = false;
                                        } else {
                                            new $().Overlays({data: {text: '批量通过失败，请稍后重试！', state: 'No'}});
                                            state = true;
                                        }
                                    }
                                })
                            }

                        })
                    }
                };
                if (data) {
                    for (var i = 0; i < data.content.length; i++) {
                        (function () {
                            var index = i;
                            var val = {
                                id: data.content[i].id,
                                result: data.content[i].driverresult == 1 && data.content[i].idcardbackresult == 1 && data.content[i].idcardpositiveresult == 1 ? '通过' : '未通过',
                                name: data.content[i].name,
                                phone: data.content[i].phone,
                                idcard: data.content[i].idcard,
                                driver: data.content[i].drivernumber,
                                photourl: '',
                                registrationtime: data.content[i].registrationtime ? $().Timestamp.TimestampDate(data.content[i].registrationtime) : '无',
                                action: ''
                            };
                            var photourls;
                            if (data.content[i].photourl && data.content[i].photourl.length > 0) {
                                photourls = data.content[i].photourl.split('&:&');
                            } else {
                                photourls = [];
                            }


                            var action = $('<div class="AuditButton"></div>');
                            action.ActionButtons({
                                data: '审核', onChange: function () {
                                    window.location.href = ctx + '/information/pass?id=' + data.content[index].id;
                                }
                            });
                            val.action = action;
                            val.photourl = '<div class="CertificateImg">';
                            for (var j = 0; j < photourls.length; j++) {
                                val.photourl += '<img  src="' + photourls[j] + '">';
                            }
                            val.photourl += '</div>';
                            OrderOption.data.Tbody.push(val)
                        })()

                    }
                }

                if (!Refresh) {//要不要刷新
                    $('.Top').find('.CustomOrder').remove();
                    $('.Top').parent().find('.PagingMdPagingMd').remove();
                }
                var PagingMdOption = {
                    data: {
                        rows: limit,
                        nowrows: index,
                        Pages: '',
                        total: data.totalElements,
                        change: function (index) {
                            PagingMd(index, SEOVal);
                        }
                    }
                };
                $('.Top').Order(OrderOption);
                $('.Top').parent().PagingMd(PagingMdOption);
                $('.BatchPass').css({display: 'block'});

            },
            error: function () {
                $('.Top').find('.CustomOrder').remove();

                var OrderOption = {//表格数据参数
                    data: {
                        Thead: {
                            result: '是否通过',
                            phone: '手机',
                            name: '姓名',
                            idcard: '身份证',
                            drivernumber: '驾驶证',
                            photourl: '证件照',
                            registrationtime: '注册时间',
                            reviewer: '审核人',
                            action: '操作'
                        },
                        Tbody: []
                    },
                    OrderH: '500px'
                };
                $('.Top').Order(OrderOption);
                $('.BatchPass').css({display: 'none'});
            }

        })
    }

    PagingMd(1, SEOVal, false, State);

    $('.SE0Button').on('click', function () {
        SEOVal = $('.SEOVal').val();

        PagingMd(1, SEOVal, false, State);
    });

    $(document).keyup(function (event) {

        switch (event.keyCode) {
            case 13:
                $(".SE0Button").click();
                break;
        }
    });

    $('.MessageTable .SEO select').on('change', function () {
        State = $(this).val();
        console.log(State)
    })

});
